<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    width="80%"
    :visible.sync="visible">
    <div>

      <el-form :model="dataForm" v-loading="loading"  ref="dataForm"
               @keyup.enter.native="dataFormSubmit()" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="险种代码" prop="crtable">
              <el-input v-model="dataForm.crtable" placeholder="险种代码"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-row style="margin-bottom: 10px;">
        <div class="title">配置条件</div>
        <el-col :span="4">
          <el-button type="primary" @click="addItem()">新增</el-button>
        </el-col>
      </el-row>

      <el-form ref="dataItemForm" :model="itemDataForm" label-width="80px">
        <el-table
          :data="itemDataForm.itemData"
          height="600"
          border
          style="width: 100%">
          <el-table-column
            prop="startTime"
            label="版本起期"
            width="180">
            <template slot-scope="scope">
              <el-form-item>
                <el-input v-model="scope.row.startTime" placeholder="版本起期"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            prop="cltdobCon"
            label="出险年龄条件">
            <template slot-scope="scope">
              <el-form-item class="tb-colum">
                <el-select v-model="scope.row.cltdobCon" filterable placeholder="请选择">
                  <el-option
                    v-for="item in eventDateProperties.cltdobConProperties"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            prop="clmType"
            label="理赔形态">
            <template slot-scope="scope">
              <el-form-item class="tb-colum">
                <el-select v-model="scope.row.clmType" filterable placeholder="请选择">
                  <el-option
                    v-for="item in eventDateProperties.claimTypeProperties"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            prop="acdtDteCon"
            label="意外事故日">
            <template slot-scope="scope">
              <el-form-item class="tb-colum">
                <el-select v-model="scope.row.acdtDteCon" filterable placeholder="请选择">
                  <el-option
                    v-for="item in eventDateProperties.acdtDteConProperties"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            prop="earlyHsDte"
            label="入院日期是否为空">
            <template slot-scope="scope">
              <el-form-item class="tb-colum">
                <el-select v-model="scope.row.earlyHsDte" filterable placeholder="请选择">
                  <el-option
                    v-for="item in eventDateProperties.earlyHsDteProperties"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            prop="eventDate"
            label="事故认定日">
            <template slot-scope="scope">
              <el-form-item class="tb-colum" :rules="itemRule.eventDate"
                            :prop="'itemData.'+scope.$index+'.eventDate'">
                <el-select v-model="scope.row.eventDate" filterable placeholder="请选择">
                  <el-option
                    v-for="item in eventDateProperties.eventDateProperties"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small"
                         @click.native.prevent="deleteItem(scope.$index, itemDataForm.itemData)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {saveOrUpdate, getInfo} from '@/api/eventdate'
  import constant from '@/api/constant'


  export default {
    data() {
      return {
        loading: false,
        visible: false,
        dataForm: {
          crtable: "",//'险种代码',
        },
        eventDateProperties: constant.eventDate,
        itemDataForm: {
          itemData: []
        },
        dataRule: {
          crtable: [
            { required: true, message: '险种代码不能为空', trigger: 'blur' }
          ]
        },
        itemRule: {
          eventDate: [
            {required: true, message: '意外事故日不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    created() {

    },
    methods: {
      init(crtable) {
        this.dataForm.crtable = crtable
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.crtable) {
            let params = {
              crtable: this.dataForm.crtable + ''
            }
            getInfo(params).then(res => {
              if (res.data.data && res.data.resultCode === 0) {
                this.itemDataForm.itemData = res.data.data
              }
            })
          }
        })
      },


      // 新增属性
      addItem() {
        let item = {
          crtable: "",//'险种代码',
          startTime: "",//'版本起期',
          cltdobCon: "",//'出险年龄条件1：出险年龄大于65',
          clmType: "",//'理赔形态',
          acdtDteCon: "",//'意外事故日\r\n1：等于0\r\
          earlyHsDte: "",//'入院日期是否为空\r\n1.是，\
          eventDate: "",//'事故认定日',
        }
        this.itemDataForm.itemData.push(item)
      },

      deleteItem(index, rows) {
        rows.splice(index, 1)
      },

      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$refs['dataItemForm'].validate((valid1) => {
              if (valid1) {
                let params = {
                  crtable: this.dataForm.crtable,
                  list: JSON.stringify(this.itemDataForm.itemData)
                }
                this.loading = true
                saveOrUpdate(params).then(res => {
                  this.loading = false
                  if (res.data && res.data.resultCode === 0) {
                    this.$message({
                      message: '操作成功',
                      type: 'success',
                      duration: 1500,
                      onClose: () => {
                        this.visible = false
                        this.dataForm = {
                          crtable: "",
                        }
                        this.itemDataForm.itemData = []
                        this.$emit('refreshDataList')
                      }
                    })
                  } else {
                    this.$message.error(res.data.resultMsg)
                  }
                })
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .title {
    padding: 10px;
    background-color: rgba(248, 248, 248, 1);
    margin-bottom: 10px;
  }

  .el-table__row .el-form-item__content {
    margin-left: 0 !important;
  }

  .el-table__row .el-form-item {
    margin-bottom: 0 !important;
  }
</style>
